package com.only4play.practice.pay.reconcile.core.entity;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.annotation.TableName;
import com.only4play.codegen.processor.api.*;
import com.only4play.codegen.processor.creator.GenCreator;
import com.only4play.codegen.processor.creator.IgnoreCreator;
import com.only4play.codegen.processor.dto.GenDto;
import com.only4play.codegen.processor.query.GenQuery;
import com.only4play.codegen.processor.updater.GenUpdater;
import com.only4play.codegen.processor.updater.IgnoreUpdater;
import com.only4play.codegen.processor.vo.GenVo;
import com.only4play.codegen.support.annotation.FieldDesc;
import com.only4play.codegen.support.constants.ValidStatus;
import com.only4play.codegen.support.model.BaseEntity;
import com.only4play.practice.common.enums.pay.MerchantNotifyType;
import com.only4play.practice.common.enums.pay.OutPayChannelType;
import com.only4play.practice.common.enums.pay.PayModeType;
import com.only4play.practice.pay.reconcile.core.model.payconfig.AliPayConfig;
import com.only4play.practice.pay.reconcile.core.model.payconfig.PayConfig;
import com.only4play.practice.pay.reconcile.core.model.payconfig.WechatPayConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author mengdy
 */
@GenCreator(pkgName = "com.only4play.practice.pay.reconcile.model.creator")
@GenUpdater(pkgName = "com.only4play.practice.pay.reconcile.model.updater")
// findById方法
@GenVo(pkgName = "com.only4play.practice.pay.reconcile.model.vo")
// findByPage方法
@GenQuery(pkgName = "com.only4play.practice.pay.reconcile.model.query")
@GenCreateRequest(pkgName = "com.only4play.practice.pay.reconcile.model.request")
@GenUpdateRequest(pkgName = "com.only4play.practice.pay.reconcile.model.request")
@GenQueryRequest(pkgName = "com.only4play.practice.pay.reconcile.model.request")
@GenResponse(pkgName = "com.only4play.practice.pay.reconcile.model.response")
@GenDto(pkgName = "com.only4play.practice.pay.reconcile.model.dto")

@GenFeign(pkgName = "com.only4play.practice.pay.reconcile.model.feign")
//
//// 生成serviceImpl
//@GenObjectMapper(pkgName = "com.only4play.practice.pay.reconcile.entitymapper")
//// 生成serviceImpl
//@GenMapper(pkgName = "com.only4play.practice.pay.reconcile.core.mapper")
//@GenService(pkgName = "com.only4play.practice.pay.reconcile.core.service")
//@GenServiceImpl(pkgName = "com.only4play.practice.pay.reconcile.core.service.impl")
//
//@GenController(pkgName = "com.only4play.practice.pay.reconcile.core.controller")

@EqualsAndHashCode(callSuper = true)
@Data
@TableName("pay_merchant_app")
public class MerchantApp extends BaseEntity {

    @FieldDesc(name = "所属企业")
    private Long enterpriseId;

    @FieldDesc(name = "支付渠道接入点编号")
    private String channelPointCode;

    /** 商户号[渠道后台配置获取] 如果支付渠道是微信，则 mchNo 存的是微信支付的商户号mchId
     * 如果支付渠道是支付宝，则 mchNo 存的是支付宝的商户号pid
     * */
    private String mchNo;

    /** 商户名称[平台设置] */
    private String mchName;
    /**
     * 支付渠道
     * @see OutPayChannelType
     */
    private OutPayChannelType channelType;
    @FieldDesc(name = "支付渠道支付模式")
    private PayModeType channelPayModeType;

    /** 开启自动对账任务 */
    private ValidStatus enableReconcile;

    /**  对账周期 值为1 表示 T + 1周期，以此类推 */
    private Integer reconcileCycle;

    // 渠道支付配置内容
    private String channelPayConfig;


    /**
     * 异步消息通知类型, 当前只支持http方式
     * @see MerchantNotifyType
     */
    private MerchantNotifyType notifyType;

    /** 是否启用状态 */
    @IgnoreUpdater
    @IgnoreCreator
    private ValidStatus validStatus;

    public void init() {
        setValidStatus(ValidStatus.VALID);
    }

    public void valid(){
        setValidStatus(ValidStatus.VALID);
    }

    public void invalid(){
        setValidStatus(ValidStatus.INVALID);
    }

    public PayConfig getPayConfig(){
        if (channelType == null){
            return null;
        }
        if (channelType == OutPayChannelType.ALIPAY){
            return new Gson().fromJson(channelPayConfig, AliPayConfig.class);
        }else if (channelType == OutPayChannelType.WECHAT){
            return new Gson().fromJson(channelPayConfig, WechatPayConfig.class);
        }
        return null;
    }
}
